<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.memberName" placeholder="会员姓名/会员电话" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="dataForm.authorName" placeholder="创建人" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-date-picker
          v-model="dataForm.glassesTime"
          type="datetime"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="配镜时间"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button
          v-if="isAuth('bus:optometryrecord:save')"
          type="primary"
          @click="addOrUpdateHandle()"
        >新增</el-button>
        <el-button
          v-if="isAuth('bus:optometryrecord:delete')"
          type="danger"
          @click="deleteHandle()"
          :disabled="dataListSelections.length <= 0"
        >批量删除</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      :span-method="objectSpanMethod"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;"
    >
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
      <!-- <el-table-column prop="id" header-align="center" align="center" label="id"></el-table-column> -->
      <el-table-column prop="memberName" header-align="center" align="center" label="会员姓名" ></el-table-column>
      <el-table-column prop="mobile" header-align="center" align="center" label="会员电话" ></el-table-column>
      <el-table-column prop="leftRightName" header-align="center" align="center" label="左右" ></el-table-column>
      <el-table-column prop="sph" header-align="center" align="center" label="球镜"></el-table-column>
      <el-table-column prop="columnMirror" header-align="center" align="center" label="柱镜"></el-table-column>
      <el-table-column prop="axial" header-align="center" align="center" label="轴位"></el-table-column>
      <el-table-column prop="pd" header-align="center" align="center" label="瞳距"></el-table-column>
      <el-table-column prop="remark" header-align="center" align="center" label="备注"></el-table-column>
      <!-- <el-table-column prop="state" header-align="center" align="center" label="状态（1：有效；0：无效）"></el-table-column> -->
      <el-table-column prop="authorName" header-align="center" align="center" label="创建人"></el-table-column>
      <el-table-column
        prop="glassesTime"
        header-align="center"
        align="center"
        label="配镜时间"
        width="160"
      ></el-table-column>
      <!-- <el-table-column prop="createTime" header-align="center" align="center" label="创建时间"></el-table-column> -->
      <!-- <el-table-column prop="modifier" header-align="center" align="center" label="修改人"></el-table-column> -->
      <!-- <el-table-column prop="modifyTime" header-align="center" align="center" label="修改时间"></el-table-column> -->
      <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
import AddOrUpdate from "./optometryrecord-add-or-update";
export default {
  data() {
    return {
      dataForm: {
        memberName: "",
        glassesTime: "",
        authorName: ""
      },
      dataList: [],
      spanArr: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false
    };
  },
  components: {
    AddOrUpdate
  },
  activated() {
    this.getDataList();
  },
  methods: {
    // 获取数据列表
    getDataList() {
      this.dataList = [];
      this.spanArr = [];
      this.dataListLoading = true;
      this.$http({
        url: this.$http.adornUrl("/bus/optometryrecord/list"),
        method: "get",
        params: this.$http.adornParams({
          page: this.pageIndex,
          limit: this.pageSize,
          memberName: this.dataForm.memberName,
          glassesTime: this.dataForm.glassesTime,
          authorName: this.dataForm.authorName
        })
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list;
          this.totalPage = data.page.totalCount;

          let contactDot = 0;
          this.dataList.forEach( (item,index) => {
            if(index===0){
              this.spanArr.push(1)
            }else{
              if(item.memberName === this.dataList[index-1].memberName){
                this.spanArr[contactDot] += 1;
                this.spanArr.push(0)
              }else{
                this.spanArr.push(1);
                contactDot = index;
              }
            }
          })
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    objectSpanMethod({row, column, rowIndex, columnIndex}){
      if(columnIndex < 3 || columnIndex > 7){
        //每次走到第2列时给单元格加上rowspan和colspan属性
        const _row = this.spanArr[rowIndex];//从处理完的数组里获取
        const _col = _row>0?1:0;
        return {
          rowspan:_row,
          colspan:_col //相当于给给表格加上rowspan,colspan属性
        }
      }
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id);
      });
    },
    // 删除
    deleteHandle(id) {
      var ids = id
        ? [id,id+1]
        : this.dataListSelections.map(item => {
            return [item.id,item.id+1];
          });
      this.$confirm(`确定要进行[${id ? "删除" : "批量删除"}]操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl("/bus/optometryrecord/delete"),
          method: "post",
          data: this.$http.adornData(ids, false)
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {
                this.getDataList();
              }
            });
          } else {
            this.$message.error(data.msg);
          }
        });
      });
    }
  }
};
</script>
